package com.codeavatar.hardwaretester.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.codeavatar.catools.adapter.CaRecyclerViewAdapter;
import com.codeavatar.catools.helper.CaLogHelper;
import com.codeavatar.hardwaretester.R;
import com.codeavatar.hardwaretester.activity.hardware.bluetooth.ServerActivity;
import com.codeavatar.hardwaretester.model.BluetoothCharacteristicModel;
import com.codeavatar.hardwaretester.model.BluetoothDeviceModel;
import com.codeavatar.hardwaretester.model.BluetoothServiceModel;

import java.util.ArrayList;
import java.util.List;

/**
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2022
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 **/
public class BluetoothCharacteristicsListAdapter extends CaRecyclerViewAdapter {

    private final String TAG = this.getClass().getSimpleName();

    private Context context;
    private RecyclerView recyclerView;
    private List<BluetoothCharacteristicModel> dataList = new ArrayList<>();;

    public BluetoothCharacteristicsListAdapter(@NonNull Context context, @NonNull RecyclerView recyclerView){
        this.context = context;
        this.recyclerView = recyclerView;
    }

    //++++++++++++++++++++++++++++++++++++++
    //++ 外部方法
    //++++++++++++++++++++++++++++++++++++++

    public void setDataList(List<BluetoothCharacteristicModel> dataList){
        if(null != dataList && dataList.size() > 0){
            this.dataList.clear();
            this.dataList.addAll(dataList);
        }
    }

    public void appendDataList(List<BluetoothCharacteristicModel> dataList){
        if(null != dataList && dataList.size() > 0){
            this.dataList.addAll(dataList);
        }
    }

    //++++++++++++++++++++++++++++++++++++++
    //++ 重写方法
    //++++++++++++++++++++++++++++++++++++++

    @Override
    public void bindView(RvViewHolder holder, int position) {
        BluetoothCharacteristicModel mm = dataList.get(position);
        TextView tvw_option = holder.getView(R.id.tvw_option);
        tvw_option.setText(String.format("%s（%s、%s、%s、%s）",mm.getUuid(),
                (mm.isRead()?"可读、":"禁读"),(mm.isWrite()?"可写、":"禁写"),(mm.isNotify()?"通知、":"禁通"),(mm.isIndicate()?"指示、":"禁示")));
        if(mm.getCheckedIndex() != -1){
            tvw_option.setTextColor(context.getColor(R.color.ca_color_white));
            tvw_option.setBackgroundColor(context.getColor(R.color.ca_color_blue));
        }else{
            tvw_option.setTextColor(context.getColor(R.color.black));
            tvw_option.setBackgroundColor(context.getColor(R.color.ca_color_white));
        }
    }

    @Override
    public void bindClick(View v, int position) {
//        BluetoothCharacteristicModel mm = dataList.get(position);
        for (BluetoothCharacteristicModel item: dataList){
            item.setCheckedIndex(-1);
        }
        dataList.get(position).setCheckedIndex(position);
        notifyDataSetChanged();
        if(context instanceof ServerActivity){
            Bundle extras = new Bundle();
            extras.clear();
            extras.putString("action","ble_connect");
            extras.putString("type","character");
            extras.putString("uuid",dataList.get(position).getUuid());
            extras.putInt("position",position);
            //向蓝牙服务广播消息
            ((ServerActivity)context).sendBleMsg(extras);
        }
    }

    @Override
    public void bindChildClick(View v, int position) {
        Bundle extras = new Bundle();
        switch (v.getId()){
            default:
                CaLogHelper.warn(TAG, context.getResources().getString(R.string.ca_txt_no_resid));
                break;
        }
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public int getResId() {
        return R.layout.layout_bluetooth_characteristic_list_item;
    }

    @Override
    public Context getContext() {
        return null;
    }

    @Override
    public boolean getClickable() {
        return true;
    }
}
